using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CodeStory.RichWebEditor
{
	public abstract class ToolbarItem : WebControl
	{
		private WebEditor _owner;

		public ToolbarItem() {
		}

		public ToolbarItem(WebEditor owner) {
			this._owner = owner;
		}

		/// <summary>
		/// 
		/// </summary>
		public abstract string ToolbarItemName { get; }

		

		[DefaultValue("")]
		public string ImageUrl {
			get {
				object obj = ViewState["ImageUrl"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["ImageUrl"] = value; }
		}

		[DefaultValue("")]
		public string ClassName {
			get {
				object obj = ViewState["ClassName"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["ClassName"] = value; }
		}
		[DefaultValue("")]
		public string HoverClassName {
			get {
				object obj = ViewState["HoverClassName"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["HoverClassName"] = value; }
		}
		[DefaultValue("")]
		public string ClickCommandName {
			get {
				object obj = ViewState["ClickCommandName"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["ClickCommandName"] = value; }
		}
		public WebEditor Owner {
			get { return _owner; }
		}

		internal void SetOwner(WebEditor owner) {
			this._owner = owner;
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer) {
			writer.AddAttribute("onmouseover", "javascript:;");
			writer.AddAttribute("onmouseout", "javascript:;");
			base.AddAttributesToRender(writer);
		}

		protected override void CreateChildControls() {
			//base.CreateChildControls();
			ChildControlsCreated = true;
			Image img = new Image();
			Controls.Add(img);
			img.ImageUrl = ImageUrl;
		}

		protected void RegisterLazyLoadScriptFiles(params string[] files) {
			StringBuilder sb = Owner.LazyLoadScriptFilesScript;
			foreach (string f in files) {
				sb.Append("CodeStory.Editor.RegisterClientScript('").Append(ToolbarItemName).Append("','").Append(f).Append("');\n\r");
			}
		}

		protected string GetClientInvokeScript(string methodname, string args) {
			return string.Format("CodeStory.Editor.Invoke('{0}','{1}',{2})", ToolbarItemName, methodname, args);
		}
	}
}